package com.example.tired1.zyfypt_14120200131_syn.model;



import com.example.tired1.zyfypt_14120200131_syn.bean.ArticleBean;
import com.example.tired1.zyfypt_14120200131_syn.iface.ArticleIface;
import com.example.tired1.zyfypt_14120200131_syn.iface.ArticleListener;
import com.example.tired1.zyfypt_14120200131_syn.service.ArticleService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by tired1 on 2017/4/20.
 */

public class ArticleModel implements ArticleIface {
    Retrofit retrofit;
    private  static  String BASEURL="http://amicool.neusoft.edu.cn/";
    private  static  String HeadPhotoNameDefault="user_headphoto.png";

    public ArticleModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener listener) {
        System.out.println("==>>"+"come in getResultList()");
        //通过该方法对网络发起请求
        ArticleService articleService =retrofit.create(ArticleService.class);
        Call<List<ArticleBean>>call=articleService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if (response.body()!=null && response.isSuccessful()){
                    listener.onResponse(response.body());
                    //使用activity中实现的接口对象，将异步返回的结果，返回到activity中处理
                }
                else {
                    System.out.println("==>>"+"response fall!");
                    listener.onFail("onResponse fail");//使用了Java的回调机制

                }
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                System.out.println("==>>"+"response onFail!");
                listener.onFail(t.toString());

            }
        });

    }
}
